<template>
  <div>
    <!--面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <!-- 搜索与添加区域 -->

      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.searchKey" clearable @clear="getUserList">
            <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
          </el-input>
        </el-col>
        <!-- <el-col :span="4">
          <el-button type="primary">添加用户</el-button>
        </el-col> -->
      </el-row>
      <!-- 用户列表区域 -->
      <el-table :data="userlist" style="width: 100%" border stripe>
        <el-table-column label="#" type="index" width="width"></el-table-column>

        <el-table-column prop="userName" label="真名" width="width"></el-table-column>
        <el-table-column prop="petName" label="昵称" width="width"></el-table-column>
        <el-table-column prop="orderUserName" label="下单户名" width="width"></el-table-column>
        <el-table-column prop="telephone" label="电话" width="width"></el-table-column>
        <el-table-column prop="regDate" label="注册时间" width="width">
        </el-table-column>
        <el-table-column label="客户类型" width="width">
          <template slot-scope="scope">
            <el-tag type='danger'>{{scope.row.userTypeName}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="memo" label="备注" width="width"></el-table-column>
        <el-table-column prop="userFaceLink" label="头像" width="width">
          <template slot-scope="scope">
            <el-avatar size="large" :src="scope.row.userFaceLink"></el-avatar>
          </template>
        </el-table-column>

        <el-table-column label="下单权限" width="width">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.validFlag=='1'?true:false" @change="updatejurisdiction(scope.row)" :disabled="extent"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="width">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editDialog(scope.row)" :disabled="extent"></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deluser(scope.row)" :disabled="extent"></el-button>
            <!-- 分配角色按钮 -->
            <!-- <el-tooltip effect="dark" content="分配角色" placement="top" :enterable="false">
              <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
            </el-tooltip> -->
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <!--修改用户的对话框-->
    <el-dialog title="修改用户" :visible.sync="editDialogVisable" width="50%">
      <el-form :model="edituser" ref="editFormRef" label-width="70px">
        <el-form-item label="真名" prop="userName">
          <el-input v-model="edituser.userName"></el-input>
        </el-form-item>
        <el-form-item label="下单户名" prop="orderUserName">
          <el-input v-model="edituser.orderUserName"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="memo">
          <el-input v-model="edituser.memo"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisable = false">取 消</el-button>
        <el-button type="primary" @click="updateuserclick">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
//表格的加载框

export default {
  data() {
    return {
      //获取用户列表的参数对象
      queryInfo: {
        searchKey: "",
        //当前页数
        pagenum: 1,
        //当前每页显示多少条数据
        pagesize: 5,
      },
      //修改用户的按钮打开嵌套的表单
      editDialogVisable: false,
      userlist: [],
      total: 0,
      //删除客户的对象
      deleteuser: {
        userId: [],
        type: "wpAppUser",
      },
      //修改用户的对象
      edituser: {
        userId: "",
      },
      //是否有权限删除
      extent: false,
    };
  },
  created() {
    this.getUserList();
    //如果checkFlag不等于2没有权限执行
    let checkFlag = window.sessionStorage.getItem("checkFlag");
    if (checkFlag != 2) {
      this.extent = true;
    }
  },
  methods: {
    //获取用户列表的请求
    async getUserList() {
      const { data: res } = await this.$http.post(
        "weixin/get_user_list.php",
        this.queryInfo
      );
      console.log(res.data);
      //console.log(this.queryInfo.pagesize);
      if (res.code !== 0) return this.$message.error("获取用户列表失败");
      this.userlist = res.data;
      this.total = res.totalLines;

      //console.log(res);
    },
    //删除用户的请求
    async deleteusers() {
      const { data: res } = await this.$http.post(
        "admin/del_admin_user.php",
        this.deleteuser
      );
      if (res.code !== 0) {
        return this.$message.error("删除用户失败");
      } else if (res.code == 0) {
        return this.$message.success("删除用户成功");
      }
    },
    //监听pagesize改变的事件
    handleSizeChange(newSize) {
      //console.log(newSize);
      this.queryInfo.pagesize = newSize;
      this.getUserList();
    },
    //监听页码值改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage);
      this.queryInfo.pagenum = newPage;
      this.getUserList();
    },
    //删除客户的按钮
    deluser(row) {
      {
        this.$confirm("此操作将删除该用户, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.deleteuser.userId.push(row.userId);
            this.deleteusers();
            this.deleteuser.userId.splice(0);

            //等待数据库更新时间然后获取列表
            setTimeout(this.getUserList, 100);
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      }
    },
    //修改客户信息的请求
    async updateuser() {
      const { data: res } = await this.$http.post(
        "admin/edit_user.php",
        this.edituser
      );
      if (res.code !== 0) {
        return this.$message.error("修改用户失败");
      } else if (res.code == 0) {
        return this.$message.success("修改用户成功");
      }
    },
    //修改客户下单权限的滑块
    updatejurisdiction(row) {
      this.edituser.userId = row.userId;
      this.$set(this.edituser, "validFlag", row.validFlag == 1 ? 0 : 1);
      this.updateuser();
      this.$delete(this.edituser, "validFlag");
      //等待数据库更新时间
      setTimeout(this.getUserList, 70);

      //console.log(row.userId);
    },
    //打开模态框并把数据传入
    editDialog(row) {
      this.editDialogVisable = true;
      this.edituser.userId = row.userId;
      this.$set(this.edituser, "userName", row.userName);
      this.$set(this.edituser, "orderUserName", row.orderUserName);
      this.$set(this.edituser, "memo", row.memo);
    },
    //修改用户其他信息
    updateuserclick() {
      this.updateuser();
      this.$delete(this.edituser, "userName");
      this.$delete(this.edituser, "orderUserName");
      this.$delete(this.edituser, "memo");
      //等待数据库更新时间
      setTimeout(this.getUserList, 70);
      this.editDialogVisable = false;
    },
  },
};
</script>

<style scoped>
</style>